<template>
  <div class="dialog" v-show="visible">
    <div class="dialog-wrap">
      <div class="dialog-header">
        <slot name="header"></slot>
        <button
          class="dialog-header-close"
          @click="$emit('update:visible', false)"
        >
          X
        </button>
      </div>
      <div class="dialog-body">
        <slot></slot>
      </div>
      <div class="dialog-footer">
        <slot name="footer"></slot>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: 'Dialog',
  props: {
    visible: {
      type: Boolean, //数据类型
      required: true, //必须传
    },
  },
  watch: {
    visible: {
      immediate: true,
      handler() {
        if (this.visible) {
          document.documentElement.style.overflow = 'hidden'
        } else {
          document.documentElement.style.overflow = 'auto'
        }
      },
    },
  },
}
</script>

<style lang="less" scoped>
.dialog {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: rgba(0, 0, 0, 0.3);
  .dialog-wrap {
    padding: 16px;
    box-sizing: border-box;
    width: 28%;
    // height: 200px;
    background-color: #fff;
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    .dialog-header {
      display: flex;
      justify-content: space-between;
      font-size: 18px;
      .dialog-header-close {
        border: none;
        background-color: #fff;
        cursor: pointer;
      }
    }
    .dialog-body {
      text-align: center;
      font-size: 16px;
      padding: 10px;
    }
  }
}
</style>
